package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据:{}", orders);

        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单分页查询
     * @param page 页码
     * @param pageSize 每页大小
     * @return R<Page<OrdersDto>>
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> showOrdersFront(Integer page, Integer pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Orders::getUserId, BaseContext.getCurrentId());
        lqw1.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage,lqw1);
        // 获取到了当前用户的订单集合
        List<Orders> orders = ordersPage.getRecords();

        // 要将订单orders集合对象映射成ordersDto集合对象
        //他们中间还差 userName phone address consignee 和orderDetail集合

        // 首先通过当前登录用户的id获取user对象
        LambdaQueryWrapper<User> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(User::getId, BaseContext.getCurrentId());

        User user = userService.getOne(lqw2);


        List<OrdersDto> ordersDtos = orders.stream().map(order -> {
            LambdaQueryWrapper<OrderDetail> lqw3 = new LambdaQueryWrapper<>();
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order, ordersDto);
            ordersDto.setUserName(user.getName());
            ordersDto.setPhone(user.getPhone());
            lqw3.eq(order.getId()!=null,OrderDetail::getOrderId, order.getId());
            ordersDto.setOrderDetails(orderDetailService.list(lqw3));
            return ordersDto;
        }).collect(Collectors.toList());



        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");
        ordersDtoPage.setRecords(ordersDtos);

        return R.success(ordersDtoPage);
    }








//    @GetMapping("/userPage")
    public R<Page<OrdersDto>> list(Integer page, Integer pageSize) {
        Long userId = BaseContext.getCurrentId();//获取当前用户id
        Page<Orders> ordersPage = new Page<>(page, pageSize);
//        Page<OrderDetail> orderDetailPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();//新建一个传递对象的page对象

        LambdaQueryWrapper<Orders> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(userId != null, Orders::getUserId, userId);
        lqw1.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(ordersPage, lqw1);
        List<Orders> ordersList = ordersPage.getRecords();//获取订单的集合

//        把orders中的数据先赋值到传递对象上
        List<OrdersDto> ordersDtos = ordersList.stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);//把基本的信息复制到传递对象上
            Long ordersId = orders.getId();
            LambdaQueryWrapper<OrderDetail> qw = new LambdaQueryWrapper<>();
            qw.eq(ordersId != null, OrderDetail::getOrderId, ordersId);
            List<OrderDetail> details = orderDetailService.list(qw);//用订单id找到对应的订单细节
            ordersDto.setOrderDetails(details);//把订单细节也写到传递对象上去
            return ordersDto;
        }).collect(Collectors.toList());
        //找到订单数据的id 用id找到对应的订单细节对象 也赋值到传递类中
        ordersDtoPage.setRecords(ordersDtos);//把所有的信息都传递过去
        return R.success(ordersDtoPage);
    }
}
